package com.victor.controller;

import static org.hamcrest.CoreMatchers.is;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.TicketOrders;
import com.victor.service.TicketOrderService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("ticketorder")
public class TicketOrderController {
	@Autowired
	private TicketOrderService ticketOrderService;
	@Autowired
	private LogController logcontroller;

	/**
	 * 票务列表 和 根据订单状态 和 使用情况
	 * @param page
	 * @param row
	 * @param flag
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg findAll(int page,int row,String flag,String useflag,String adminid) {
		HashMap<Object, Object> map = new HashMap<>();
		if (flag != null && !flag.isEmpty()) {
			map.put("flag", flag);
		}
		if (useflag != null && !useflag.isEmpty()) {
			map.put("useflag", useflag);
		}
		PageInfo<TicketOrders> pageInfo = ticketOrderService.selAll(page,row,map);
		logcontroller.addLog(adminid, "根据订单状态和使用情况搜索和查询了票务");
		return Msg.success().add("list", pageInfo);
	}
	/**
	 * 搜索
	 * @param number
	 * @return
	 */
	@RequestMapping("/search")
	public Msg search(String searchname,int page,int row,String adminid) {
		if (searchname == null || searchname.isEmpty()) {
			return Msg.fail("入参错误");
		}
		PageInfo<TicketOrders> ticketOrders = ticketOrderService.selOrderByNumber(searchname,page,row);
		logcontroller.addLog(adminid, "根据"+searchname+"，搜索了票务列表");
		return Msg.success().add("ticketOrders", ticketOrders);
		
	}
	
	/**
	 * 票务详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg Xiangqing(Integer id,String adminid) {
		if (id == null ) {
			return Msg.fail("入参失败");
		}
		TicketOrders ticketOrders = ticketOrderService.selByOrderId(id);
		logcontroller.addLog(adminid, "查询了"+id+"的票务详情");
		return Msg.success().add("ticketOrders", ticketOrders);
		
	}
	
	/**
	 * 删除订单
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Msg delteteOrder(int[] ids,String adminid) {
		if (ids == null || ids.length<=0) {
			return Msg.fail("入参错误");
		}
		boolean b = ticketOrderService.deleteOrder(ids);
		logcontroller.addLog(adminid, "删除了票务"+ids);
		return Msg.judge(b);
	}
	/**
	 * 修改订单
	 * @param ticketOrders
	 * @return
	 */
	@RequestMapping("/updateorder")
	public Msg updateOrder(TicketOrders ticketOrders,String adminid) {
		if (ticketOrders == null || ticketOrders.getId() == null ) {
			return Msg.fail("入参失败");
		}
		boolean b = ticketOrderService.updateOrder(ticketOrders);
		logcontroller.addLog(adminid, "修改了"+ticketOrders.getId()+"的票务订单");
		return Msg.judge(b);
	}
	/**
	 * 搜索时间
	 * @param start
	 * @param end
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/searchtime")
	public Msg searchTime(String start,String end,int page,int row,String adminid) {
		if (start == null && end == null) {
			return Msg.fail("入参失败");
		}
		 PageInfo<TicketOrders> list = ticketOrderService.selByTime(start,end,page,row);
		 logcontroller.addLog(adminid, "根据时间对票务列表进行搜索");
		return Msg.success().add("list", list);
		
	}
}
